Box = require "box"
UI = require "ui"

debug = false
ui = nil
box = nil
gameOver = false
moveCount = 0

function love.load(arg)
    love.graphics.setBackgroundColor(250/255,248/255,239/255,1)
    ui = UI.new()
    ui:buttonActions()
    ui.reset = function()
        box = Box.new()
        box:update(2)
    end
    ui.reset()
end

function love.keypressed(key)
    if box:gameOver() or box:gameWin() then
        return
    end

    if key == "a" or key == "left" then
       box:left()
    elseif key == "d" or key == "right" then
       box:right()
    elseif key == "w" or key == "up" then
       box:up()
    elseif key == "s" or key == "down" then
       box:down()
    end
end

function love.mousepressed( x, y, button )
    if button == 1 then
        ui.mouse = {status="pressed", x=x, y=y, button=button}
    end
end

function love.mousereleased( x, y, button )
    if button == 1 then
        ui.mouse = nil
        ui:click(x, y)
    end
end

function love.gamepadreleased( joystick, button )
    if box:gameOver() or box:gameWin() then
        return
    end
    if button == "x" or button == "dpleft" then
        box:left()
    elseif button == "b" or button == "dpright" then
       box:right()
    elseif button == "y" or button == "dpup" then
       box:up()
    elseif button == "a" or button == "dpdown" then
       box:down()
    end
end

function love.update(dt)
end

function love.draw(dt)
    ui:update(box) 
    if box:gameWin() then
        ui:gameWin()
    elseif box:gameOver() then
        ui:gameOver()
    end
end
